<?php
class UtilityController extends CustomControllerAction {
	public function captchaAction() {
		$session = new Zend_Session_Namespace("captcha");
		$captcha = new CaptchaImage(
			array(
				'font'=>Zend_Registry::get('config')->paths->captcha->font,
                'saveToFile'=>false,
                'width'=>120,
                'height'=>40,
                'wordlen'=>4,
                'DotNoiseLevel'=>10
                )); 
        
		$this->_helper->viewRenderer->setNoRender();
        $captcha->generate();
        $session->captchaWord = $captcha->getWord();
	}
	
	public function imageAction() {
		$request = $this->getRequest();
		$response = $this->getResponse();
		
		$id = (int) $request->getQuery('id');
		$w = (int) $request->getQuery('w');
		$h = (int) $request->getQuery('h');
		$hash = $request->getQuery('hash');
		
		$realHash = DatabaseObject_Image::GetImageHash($id, $w, $h);
		
		$this->_helper->viewRenderer->setNoRender();
		$image = new DatabaseObject_Image($this->db);
		
		if ($hash != $realHash || !$image->load($id)) {
			$response->setHttpResponseCode(404);
			return;
		}
		
		try {
			$fullpath = $image->createThumbnail($w, $h);
		} catch (Exception $ex) {
			$fullpath = $image->getFullPath();
		}
		
		$info = getimagesize($fullpath);
		
		$response->setHeader('content-type', $info['mime']);
		$response->setHeader('content-type', filesize($fullpath));
		
		echo file_get_contents($fullpath);
	}
}
?>